import { getDishPageAPI } from "@/api/business/dish"
import { DishResult } from "@/types/dish"
import { useUserStore } from "@/stores/userStore"
import { ElMessage } from "element-plus"
import { ref } from "vue"
import { getAdminDishPageAPI } from "@/api/admin/dish"

export default function () {
  // 菜品名称
  let name = ref('')
  /// 菜品分类
  let categoryId = ref<number>()
  // 售卖状态
  const status = ref<number>()
  let page = ref(1)
  let pageSize = ref(10)
  let total = ref(0)
  // 菜品数据
  const dishs = ref<DishResult[]>([])
  const userStore = useUserStore()
  const getDishPage = async () => {
    const params = {
      page: page.value,
      pageSize: pageSize.value,
      businessId: userStore.rule === 'admin' ? undefined : userStore.user.id,
      name: name.value || undefined,
      categoryId: categoryId.value || undefined,
      status: status.value,
    }
    try {
      if (userStore.rule === 'admin') {
        let { data } = await getAdminDishPageAPI(params)
        dishs.value = data.records
        total.value = data.total
      } else {
        let { data } = await getDishPageAPI(params)
        dishs.value = data.records
        total.value = data.total
      }
    } catch (error) {
      ElMessage.error(error || '请求失败')
    }
  }
  return {
    name, categoryId, page, pageSize, status, total, dishs, getDishPage
  }
}